package com.qf.myblogssm.controller;

import com.qf.myblogssm.pojo.Tarticle;
import com.qf.myblogssm.pojo.Ttag;
import com.qf.myblogssm.service.ArticleService;
import com.qf.myblogssm.service.CommentService;
import com.qf.myblogssm.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by 王镜 on 2019/6/17 0017 14:45
 */
@Controller
@RequestMapping("articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private TagService tagService;

    @Autowired
    private CommentService commentService;
    @RequestMapping("/getById")
    //    public String getArticleById(Integer id, Model model){
    public String getArticleById(Integer id, HttpServletRequest request){
        //加载文章内容
       Tarticle tarticle= articleService.getArticleById(id);
        request.setAttribute("article",tarticle);
        Tarticle tarticle1=articleService.getByPreArticleId(id);
        request.setAttribute("article_pre",tarticle1);
        Tarticle tarticle2=articleService.getByNextArticleId(id);
        request.setAttribute("article_next",tarticle2);
        //model.addAttribute("article",tarticle);
        //model，modelAndView只适用于一次request，重定向之后就没有了
//        ModelAndView modelAndView=new ModelAndView();
//        modelAndView.addObject("article",tarticle);
//        modelAndView.setViewName("article");
//        return modelAndView;
       //加载标签
        List<Ttag> tags=tagService.getTagsById(id);
        request.setAttribute("article_tags",tags);
       // model.addAttribute("article_tags",tags);

        //加载评论
        request.setAttribute("comment",commentService.getCommentByAid(id));
        return "article";
    }

    @RequestMapping("/star")
    @ResponseBody
    public Tarticle star(Integer id){
        System.out.println("*******************");
        try{
            articleService.star(id);
            Tarticle tarticle=new Tarticle();
            System.out.println(tarticle.getStar());
            Tarticle article = articleService.getArticleById(id);
            return article;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Tarticle();
    }
//需要我们返回Map<String,List<Tarticle>>
    @RequestMapping("/getSortInfo")
    public String getSortInfo(Model model){
       Map<String,List<Tarticle>> sort_article_map=articleService.getSortInfo();
        model.addAttribute("sort_article_map",sort_article_map);
        return "sort";
    }
    @RequestMapping("/getSortAndArticle")
    public String getSortAndArticle(String get,HttpServletRequest request){
        List<Tarticle> tarticle= articleService.getBySort(get);
        request.setAttribute("article_list",tarticle);
        List<Ttag> tag_ist=tagService.selectAllTags();
        request.setAttribute("tag_list",tag_ist);

        Integer number=articleService.countNumber();
        request.setAttribute("article_number",number);

        Integer sortnumber=articleService.countSortNumber();
        request.setAttribute("sort_number",sortnumber);

        Integer tagnumber=tagService.countTagNumber();
        request.setAttribute("tag_number",tagnumber);

        List<Tarticle> tarticles=articleService.getArticle_sort_count();//获取文章的分类个数
        Map<String,Integer> map=new HashMap();
        for(Tarticle tarticle1:tarticles){
            map.put(tarticle1.getSort(),tarticle1.getCounts());
        }
        request.setAttribute("sort_count_map",map);
        return "main";
    }
}
